<script setup lang="ts">
import { findGoodDetail } from '@/service/api/goods'
import { IGoodsDetail } from '@/service/api/type/goods.type'
import { computed, reactive, ref, watch, provide } from 'vue'
import { useRoute } from 'vue-router'
import ProductBread from './components/product-bread.vue'
import GoodsImages from './components/goods-images.vue'
import GoodsSales from './components/goods-sales.vue'
import GoodsName from './components/goods-name.vue'
import GoodsSku from './components/goods-sku.vue'
import Numbox from '@/components/numbox/numbox.vue'
import Button from '@/components/button/button.vue'
import GoodsRelevant from './components/goods-relevant.vue'
import GoodsTabs from './components/goods-tabs.vue'
import GoodsHot from './components/goods-hot.vue'
import GoodsWarn from './components/goods-warn.vue'

const goodsDetailData = reactive({}) as IGoodsDetail

const route = useRoute()

const getGoodDetailData = async () => {
  const { result } = await findGoodDetail(route.params.id as string)
  Object.assign(goodsDetailData, result)
}
const productBreadData = computed(() => {
  return {
    id: goodsDetailData.id,
    categories: goodsDetailData.categories,
    name: goodsDetailData.name
  }
})

watch(
  () => route.params.id,
  () => {
    getGoodDetailData()
  },
  {
    immediate: true
  }
)

const changeSku = (value: any) => {
}

const count = ref(1)

//给goods-detail组件
provide('goods', goodsDetailData)
</script>

<template>
  <div class="product">
    <div class="container" v-if="goodsDetailData.id">
      <ProductBread :product-bread-data="productBreadData"></ProductBread>

      <!-- 商品信息 -->
      <div class="goods-info">
        <div class="media">
          <GoodsImages :images-list="goodsDetailData.mainPictures"></GoodsImages>
          <GoodsSales></GoodsSales>
        </div>
        <div class="spec">
          <GoodsName :goods-name-data="goodsDetailData"></GoodsName>
          <GoodsSku :goods="goodsDetailData" @change="changeSku"></GoodsSku>
          <Numbox lable="数量" v-model="count"></Numbox>
          <Button type="primary" style="margin-top: 20px;">
            <span>加入购物车</span>
          </Button>
        </div>
      </div>
      <!-- 商品推荐 -->
      <GoodsRelevant :goodsId="goodsDetailData.id"></GoodsRelevant>
      <!-- 商品详情 -->
      <div class="goods-footer">
        <div class="goods-article">
          <!-- 商品+评价 -->
          <GoodsTabs></GoodsTabs>
          <!-- 注意事项 -->
          <GoodsWarn></GoodsWarn>
        </div>
        <!-- 24热榜+专题推荐 -->
        <div class="goods-aside">
          <GoodsHot :goodsId="goodsDetailData.id" :type="1" />
          <GoodsHot :goodsId="goodsDetailData.id" :type="2" />
        </div>
      </div>
    </div>
    <div class="container none" v-else>
      <div class="loading">
        <img src="/src/assets/images/loading.gif" />
      </div>
    </div>
  </div>
</template>

<style lang="less" scoped>
.goods-info {
  min-height: 600px;
  background: #fff;
  display: flex;
  .media {
    width: 580px;
    height: 600px;
    padding: 30px 50px;
  }
  .spec {
    flex: 1;
    padding: 30px 30px 30px 0;
  }
}
.goods-footer {
  display: flex;
  margin-top: 20px;
  .goods-article {
    width: 940px;
    margin-right: 20px;
  }
  .goods-aside {
    width: 280px;
    min-height: 1000px;
  }
}

.goods-warn {
  min-height: 600px;
  background: #fff;
  margin-top: 20px;
}
.none {
  display: flex;
  justify-content: center;
  background-color: #fff;
  margin-top: 20px;
  min-height: 600px;
  align-items: center;
  .loading {
    width: 60px;
    height: 60px;
  }
}
</style>
